package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  NenNewsAnalyse  东北新闻网新闻解析类
 *  xiaonie 2012-11-22
 */
public class NenNewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://[a-z]*[.]nen.com.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[.]shtml",
			"http://house.nen.com.cn/fangchan/web/html/[0-9]*/[0-9]{8}/[0-9]*[.]shtml",
			"http://edu.nen.com.cn/jiaoyu/[0-9]*/[0-9]*[.]shtml",
			"http://dd.nen.com.cn/[0-9]*/[0-9]{8}/[0-9]*.shtml"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		
		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			if(url.startsWith("http://liaoning.nen.com.cn")){
				title=doc.select("div.inversey h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentcon"));
				dateStr=doc.select("div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://jiankang.nen.com.cn/")){
				title=doc.select("td.contitle").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.cont"));
				dateStr=doc.select("td.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://xiaofei.nen.com.cn/")){
				title=doc.select("td.contentt").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.contentcon"));
				dateStr=doc.select("div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://xy.nen.com.cn/")){
				title=doc.select("strong").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.content"));
				dateStr=doc.select("a.nava3").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://society.nen.com.cn/")){
				title=doc.select("td.contentt").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.contentconshindex"));
				dateStr=doc.select("td.contenttime").text();
				Elements selectDate=doc.select("td.contenttime");
				if(selectDate.size() > 0){
					Pattern DATE=Pattern.compile("[0-9]{1,2}-[0-9]{1,2}-[0-9]{1,2}");
					Pattern TIME=Pattern.compile("[0-9]{1,2}[:][0-9]{1,2}");
					Matcher matcher_date=DATE.matcher(selectDate.toString());
					String _date="";
					if (matcher_date.find()) {
						 String group=matcher_date.group();
						 _date="20" + group;
					}
					Matcher matcher_time=TIME.matcher(selectDate.toString());
					if (matcher_time.find()) {
						 String group=matcher_time.group();
						 _date += " "+group;
					}
					if(!"".equals(_date)){
						date=DateUtils.FormString(_date);
					}
				}
			}else if(url.startsWith("http://house.nen.com.cn/fangchan/")){
				Element element;
				try {
					title=doc.select("p.STYLE81").text();
					content=HtmlCleaner.getContentHtml(url,doc.select("table.text"));
					Elements select=doc.select("td[width=70%]");
					element=select.select("span").get(1);
					date=DateUtils.matchDate(element.toString());
				} catch (Exception e) {
				}
			}else if(url.startsWith("http://edu.nen.com.cn/jiaoyu")){
				title=doc.select("div.contentt").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentcon"));
				dateStr=doc.select("div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://news.nen.com.cn/system/")){
				title=doc.select("div.floatl h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentcon"));
				dateStr=doc.select("div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://auto.nen.com.cn/")){				
				title=doc.select("td.contentt > h3").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.contentcon"));
				dateStr=doc.select("td.contenttime div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://edu.nen.com.cn/")){				
				title=doc.select("div.conttitel h2").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.conttitel"));
				dateStr=doc.select("div.conttitel div.neirongtime").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://dd.nen.com.cn/")){				
				title=doc.select("td.nava11").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("span.nava1"));
				date=DateUtils.matchDate(doc.select("table.table36").text());
			}else{
				title=doc.select("div.contentt").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentcon"));
				dateStr=doc.select("div.contenttime").text();
				date=DateUtils.matchDate(dateStr);
			}
			if(StringUtils.isBlank(title)){
				title=doc.select("div.contentt").text();
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentcon"));
			}
			if(StringUtils.isBlank(title)){
			}
			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			news.setCommentNum(null);
			news.setDate(date);
			news.setType(1);
			
			
		}
		return news;
	}

	public static void main(String[] args) {
		NenNewsAnalyse a=new NenNewsAnalyse();
		String url="http://news.nen.com.cn/system/2015/12/07/018687051.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
